package com.springboot.chatroom.controller;


import com.springboot.chatroom.model.Friend;
import com.springboot.chatroom.model.User;
import com.springboot.chatroom.service.FriendService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: jiume
 * Date: 2025-04-10
 * Time: 17:31
 */
@Slf4j
@RestController
@RequestMapping("/friend")
public class FriendController {

    @Resource
    private FriendService friendService;

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Object getList(HttpServletRequest request) {
        // 通过http请求中携带的session信息来判断用户是否登录以及获取userId
        HttpSession session = request.getSession(false);
        if (session == null) {
            log.info("[session] 用户并未登录...");
            return new ArrayList<>();
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            log.info("[user] 用户并未登录...");
            return new ArrayList<>();
        }
        // 根据 userId 去查询数据库中的信息
        return friendService.getFriendList(user.getUserId());
    }
}
